﻿Imports System.IO

Module Module1

    Public addpagewasloaded As Boolean = False
    Public listofmandate As New List(Of Mandat)

    Private Function MyFile() As String
        'Return System.IO.Path.Combine("../ClientBin", "users.txt")
        Return Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "mandate.xml"))
    End Function

    Function CreateXML() As XDocument
        If System.IO.File.Exists(MyFile) Then
            Return XDocument.Load(MyFile)
        Else
            Dim xdoc As New XDocument
            Dim Root As XElement = <Mandates>

                                   </Mandates>
            xdoc.Add(Root)
            xdoc.Save(MyFile)
            Return xdoc
        End If
    End Function



    Sub AddToXmlFile(ByVal tag As String, ByVal stvo As String, ByVal pg As String, ByVal types As String, ByVal ray As String)
        Dim xdoc = CreateXML()

        Dim elements = From b In xdoc.Root.Elements
                       Where b.<tag>.Value = tag
                       Select b

        If elements(0) Is Nothing Then


            Dim newElement As XElement = <Mandate>
                                             <datum><%= tag %></datum>
                                             <stvo><%= stvo %></stvo>
                                             <pg><%= pg %></pg>
                                             <typ><%= types %></typ>
                                             <rayon><%= ray %></rayon>
                                         </Mandate>
            xdoc.Root.Add(newElement)
            xdoc.Save(MyFile)
        Else
        End If

    End Sub

    Sub RemoveFromXML(ByVal tag As String)
        Dim xdoc = CreateXML()

        Dim user = From a In xdoc.Root.Elements
                   Where a.<tag>.Value = tag
                   Select a



        If user(0) IsNot Nothing Then
            user(0).Remove()
            xdoc.Save(MyFile)
        End If
    End Sub

    Function ReadAllUsersFromXMLFile() As List(Of Mandat)
        Dim retlist As New List(Of Mandat)
        Dim xdoc = CreateXML()
        For Each a In xdoc.Root.Elements
            Dim nuser As New Mandat
            With nuser
                .Datum = a.<datum>.Value.ToString
                .StvoAnzahl = a.<stvo>.Value.ToString
                .PgAnzahl = a.<pg>.Value.ToString
                .Type = a.<typ>.Value.ToString
                .Rayon = a.<rayon>.Value.ToString
            End With
            retlist.Add(nuser)
        Next

        Return retlist
    End Function

    Function ReadAllUsersFromXMLFile(ByVal xdocs As XDocument) As List(Of Mandat)
        Dim retlist As New List(Of Mandat)
        Dim xdoc = xdocs
        For Each a In xdoc.Root.Elements
            Dim nuser As New Mandat
            With nuser
                .Datum = a.<datum>.Value.ToString
                .StvoAnzahl = a.<stvo>.Value.ToString
                .PgAnzahl = a.<pg>.Value.ToString
                .Type = a.<typ>.Value.ToString
                .Rayon = a.<rayon>.Value.ToString
            End With
            retlist.Add(nuser)
        Next
        Return retlist
    End Function

    Public Sub RefreshList()
        listofmandate.Clear()
        listofmandate = ReadAllUsersFromXMLFile()
    End Sub

    Function SumUpAllMandate() As Integer
        Dim summen As Integer = 0
        For Each a In listofmandate
            summen += CInt(a.PgAnzahl)
            summen += CInt(a.StvoAnzahl)
        Next
        Return summen
    End Function

    Function SumUpPG() As Integer
        Dim summen As Integer = 0
        For Each a In listofmandate
            summen += CInt(a.PgAnzahl)
        Next
        Return summen
    End Function

    Function SumUpStVo() As Integer
        Dim summen As Integer = 0
        For Each a In listofmandate
            summen += CInt(a.StvoAnzahl)
        Next
        Return summen
    End Function


#Region "searching..."

    Function SumUpSearch(ByVal searchitems As List(Of Mandat)) As SumUps
        Dim pg As Integer
        Dim stvo As Integer
        Dim gesamnt As Integer

        For Each a In searchitems
            pg += a.PgAnzahl
            stvo += a.StvoAnzahl
        Next

        Dim ntype As New SumUps
        With ntype
            .GesamtAnzahl = pg + stvo
            .PgAnzahl = pg
            .StVoAnzahl = stvo
        End With
        Return ntype
    End Function


    Public Function search(ByVal datum As String, ByVal rayon As String, ByVal type As String) As List(Of Mandat)

        Dim query As IEnumerable(Of Mandat) = listofmandate

        If datum <> "" Then
            query = query.Where(Function(f As Mandat)
                                    If f.Datum = datum Then
                                        Return True
                                    End If
                                    Return False
                                End Function)
        End If

        If rayon <> "" Then
            query = query.Where(Function(f As Mandat)
                                    If f.Rayon = rayon Then
                                        Return True
                                    End If
                                    Return False
                                End Function)
        End If

        If type <> "" Then
            query = query.Where(Function(f As Mandat)
                                    If f.Type = type Then
                                        Return True
                                    End If
                                    Return False
                                End Function)
        End If

        Dim retlist As New List(Of Mandat)

        For Each g In query
            retlist.Add(g)
        Next
        Return retlist
    End Function

 
#End Region

End Module


Public Class Mandat
    Property Datum As String
    Property StvoAnzahl As Integer
    Property PgAnzahl As Integer
    Property Type As String
    Property Rayon As String
    ReadOnly Property Gesamt As Integer
        Get
            Return StvoAnzahl + PgAnzahl
        End Get
    End Property
End Class

Public Class SumUps
    Property StVoAnzahl As Integer
    Property PgAnzahl As Integer
    Property GesamtAnzahl As Integer
End Class